package com.yangjun.service.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.ServerResponse;
import com.yangjun.common.comparator.TargetItemComparator;
import com.yangjun.common.comparator.TargetItemComparatorByTargetArr;
import com.yangjun.common.excel.DataBaseExcelContext;
import com.yangjun.common.excel.ExcelExportResult;
import com.yangjun.dao.mybatis.mapper.report.CommodityFlowReportExportMapper;
import com.yangjun.dao.mybatis.mapper.report.DvReportSearchTmpMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportDownloadStatusMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportExportStatusMapper;
import com.yangjun.dao.mybatis.mapper.report.ReportImportStatusMapper;
import com.yangjun.dao.mybatis2.mapper.common.CommonMapper;
import com.yangjun.entity.report.DvExcelFieldDefinitionEntity;
import com.yangjun.entity.report.DvReportDownloadStatusEntity;
import com.yangjun.entity.report.DvReportExportStatus;
import com.yangjun.entity.report.DwCommodityFlow;
import com.yangjun.po.report.CommodityFlowReportExportPO;
import com.yangjun.service.common.CommonService;
import com.yangjun.service.permission.ReportExportPermissionService;
import com.yangjun.utils.DvDateUtils;
import com.yangjun.utils.DvUtils;
import com.yangjun.utils.POIExcelUtils;
import com.yangjun.vo.report.CommodityFlowReportExportVO;
import com.yangjun.vo.report.CommodityFlowReportManageVO;
@Service
public class CommodityFlowReprotExportService  {
	@Value("${excel.uploadPath}")
    private String uploadPath;
	@Value("${excel.downloadPath}")
    private String downloadPath;
	@Autowired
	private DataBaseExcelContext context;
	
	private final static String reporttype = "commodityFlow";
	private final static String searchName = "wareCode";
	@Autowired
	private CommodityFlowReportExportMapper commodityFlowReportExportMapper;
	@Autowired
	private ReportExportStatusMapper reportExportStatusMapper;
	@Autowired
	private ReportImportStatusMapper reportImportStatusMapper;
	@Autowired
	private DvReportSearchTmpMapper dvReportSearchTmpMapper;
	@Autowired
	private ReportCommonService reportCommonService;
	@Autowired
	private ReportExportPermissionService reportExportPermissionService;
	@Autowired
	private ReportDownloadStatusMapper reportDownloadStatusMapper;
	@Autowired
	private CommonService commonService;
 	@Autowired
 	private CommonMapper commonMapper;
	/**
	 * @Description 获取维度信息
	 * @return
	 * @author yangjun
	 * @Date 2017年7月5日 上午11:50:06
	 */
	public Object getDeminsion(String reportid,String userid){
		CommodityFlowReportExportVO cfreVo = null;
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		
		String[] targetSelectData  = null;
		//修改报表排序号的字段顺序
		List<Map<String,Object>> sortTargetSelectData=Lists.newArrayList();
		String reportName = "商品流向-";
		String uploadFileId = "";
		String uploadFileName = "";
		String searchName = "";
		String date = DvDateUtils.getDate(new Date(), 0);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		String endDate = DvDateUtils.getDate(new Date(), 0);
		String beginDate = DvDateUtils.getDate(calendar.getTime(), 0);
		
		//平台
		String[] saveOrdersourceArr = null;
		int type = reportExportPermissionService.getManagerType(userid, reporttype);
		List<Map<String, Object>> ordersource = commonService.getOrderSource(type,userid,reporttype);
		
		if(dvReportExportStatus!=null){
			cfreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), CommodityFlowReportExportVO.class);
			beginDate = cfreVo.getBeginDate();
			endDate = cfreVo.getEndDate();
			targetSelectData = cfreVo.getTarget();
			reportName = cfreVo.getReportName();
			uploadFileId = cfreVo.getUploadFileId();
			saveOrdersourceArr = cfreVo.getOrdersource();
			if(StringUtils.isNotBlank(uploadFileId)){
				uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(uploadFileId).getReportName();
			}
			searchName = cfreVo.getSearchName();
		}
		Map<String,Object> demensionMap = Maps.newHashMap();
		demensionMap.put("reportName",reportName);
		demensionMap.put("beginDate", beginDate);
		demensionMap.put("endDate", endDate);
		demensionMap.put("date", date);
		demensionMap.put("uploadFileName", uploadFileName);
		demensionMap.put("uploadFileId", uploadFileId);
		
		//平台
		for(Map<String, Object> ordersourceItem : ordersource){
			if(saveOrdersourceArr!=null){
				ordersourceItem.put("checked", false);
				String[] os = ordersourceItem.get("ordersource").toString().split(",");
				for(String osItem : os){
					if(ArrayUtils.contains(saveOrdersourceArr, osItem)){
						ordersourceItem.put("checked", true);
					}
				}
			}else{
				ordersourceItem.put("checked", true);
			}
		}
		demensionMap.put("ordersource",ordersource);
		
		
		List<DvExcelFieldDefinitionEntity> excelFieldDefinitionList = reportCommonService.getExcelFieldDefinitionList(reporttype);

		//获取每个报表的指标分类
		Map<String,String> tmpCategoryMap = reportCommonService.queryCategory(reporttype);
		List<Map<String,String>> categoryList = Lists.newArrayList();
		
		Map<String,List<Map<String,Object>>> targetMap = Maps.newHashMap();
		List<Map<String,Object>> targetList =null;
		for(Map.Entry<String,String> categoryEntry: tmpCategoryMap.entrySet()){
			//初始化指标分类数组
			Map<String,String> categoryMap = Maps.newHashMap();
			categoryMap.put("name", categoryEntry.getKey());
			categoryMap.put("value", categoryEntry.getValue());
			categoryList.add(categoryMap);
			//初始化指标分类内容
			targetList = Lists.newArrayList();
			targetMap.put(categoryEntry.getKey(),targetList);
		}
		demensionMap.put("category", categoryList);
		//查询有权限显示的报表字段
		String[] allowFieldArr = reportExportPermissionService.getReportAllowField(userid, reporttype);
		if(allowFieldArr!=null&&allowFieldArr.length>0){
			for(DvExcelFieldDefinitionEntity excelFieldDefinitionEntity : excelFieldDefinitionList){
				//权限控制能够显示的字段，如果查询不到对应角色的权限直接不显示字段。
				//&&("18".equals(userid)||"507".equals(userid)||"715".equals(userid)||"6".equals(userid)||"518".equals(userid)||"141".equals(userid))
				if(allowFieldArr!=null){
					List<String> allowFieldList = Arrays.asList(allowFieldArr);
					if(!allowFieldList.contains(excelFieldDefinitionEntity.getEntityName())){
						continue;
					}
				}
				//封装默认的指标格式
				String categoryId = excelFieldDefinitionEntity.getCategoryId();
				if(StringUtils.isNotBlank(categoryId)){
					String entityName = excelFieldDefinitionEntity.getEntityName();
					String title = excelFieldDefinitionEntity.getTitle();
					int sort = excelFieldDefinitionEntity.getFieldSort();
					Map<String,Object> itemMap = Maps.newHashMap();
					itemMap.put("value", entityName);
					itemMap.put("name", title);
					itemMap.put("sort", sort);
					itemMap.put("checked", false);
					if(targetSelectData!=null){
						for(int i=0,len=targetSelectData.length;i<len;i++){
							if(entityName.equals(targetSelectData[i])){
								itemMap.put("checked", true);
								//修改报表排序号的字段顺序
								sortTargetSelectData.add(itemMap);
							}
						}
					}
					targetList = targetMap.get(categoryId);
					targetList.add(itemMap);
				}
			}
			for(Map.Entry<String,List<Map<String,Object>>> targetItemEntry : targetMap.entrySet()){
				Collections.sort(targetItemEntry.getValue(),new TargetItemComparator());
			}
		}

		demensionMap.put("reporttype", reporttype);
		demensionMap.put("target", targetMap);
		Collections.sort(sortTargetSelectData, new TargetItemComparatorByTargetArr(targetSelectData));
		demensionMap.put("sortTarget", sortTargetSelectData);
		
		String[] searchWayNameArr = {"商品编码"};
		String[] searchWayValueArr = {"wareCode"};
		List<Map<String,String>> searchWayList = Lists.newArrayList();
		for(int i = 0,len=searchWayNameArr.length;i<len;i++){
			Map<String,String> searchMap = Maps.newHashMap();
			searchMap.put("searchWayName", searchWayNameArr[i]);
			searchMap.put("searchWayValue", searchWayValueArr[i]);
			searchWayList.add(searchMap);
		}

		demensionMap.put("search", searchWayList);
		demensionMap.put("searchName", searchName);
		reportCommonService.deleteUploadReport(userid);
		return ServerResponse.createBySuccess(demensionMap);
	}
	public Object queryData(String reportid){
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		CommodityFlowReportExportVO cfreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), CommodityFlowReportExportVO.class);
		return queryData(cfreVo);
	}
	
	public Object queryData(CommodityFlowReportExportVO cfreVo){
		if(cfreVo.getOrdersource()==null||cfreVo.getOrdersource().length<1){
			return ServerResponse.createByErrorMessage("平台为空,请修改报表！");
		}
		//商品流向默认按商品编号搜索
		cfreVo.setSearchName(searchName);
		Map<String,Object> resultMap = Maps.newHashMap();
		cfreVo.setDate(DvDateUtils.getDate(new Date(), 0));
		String[] targetArr = cfreVo.getTarget();
		List<String> targetList = getDefaultTargetList(targetArr);
		Long start = System.currentTimeMillis();
		List<DvExcelFieldDefinitionEntity>  reportFieldDefinition= reportCommonService.getExcelFieldDefinitionList(reporttype);
		//装载表格数据
		List<List<String>> tableList = Lists.newArrayList();
		List<String> titleList = Lists.newArrayList();
		for(String item : targetList){
			for(DvExcelFieldDefinitionEntity definition : reportFieldDefinition){
				String name = definition.getEntityName();
				if(item.equals(name)){
					titleList.add(definition.getTitle());
				}
			}
		}
		tableList.add(titleList);
		System.out.println("1."+(System.currentTimeMillis()-start)/1000);

		//查找附件内容作为搜索条件
		if(StringUtils.isNotBlank(cfreVo.getUploadFileId())){
			int partSize=1000;
			List<String> searchItemList = Lists.newArrayList();
			String fileName = reportImportStatusMapper.queryReportStatusByUUID(cfreVo.getUploadFileId()).getFileName();
			File uploadFile = new File(uploadPath+fileName);
			FileInputStream readStream = null;
			try {
				readStream = new FileInputStream(uploadFile);
				List<List<Object>> datas =POIExcelUtils.readExcel(readStream, 0);
				for(int i=0,len=datas.size();i<len;i++){
					if(i>0&&datas.get(i).get(0)!=null){
						searchItemList.add(datas.get(i).get(0).toString());
					}
				}
				dvReportSearchTmpMapper.deleteDvReportSearchTmp(cfreVo.getUserid());
				if(searchItemList.size()>partSize){
					int part = searchItemList.size()/partSize;
					for(int i=0;i<part;i++){
						List<String> tmpList  = searchItemList.subList(0, partSize);
						dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(cfreVo.getUserid(), tmpList);
						searchItemList.subList(0, partSize).clear();
					}
				}
				if(searchItemList.size()>0){
					dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(cfreVo.getUserid(), searchItemList);
				}
				cfreVo.setSearchValue(searchItemList.size()>0?searchItemList.toArray(new String[searchItemList.size()]):null);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		System.out.println("2."+(System.currentTimeMillis()-start)/1000);
		CommodityFlowReportExportPO commodityFlowReportExportPO = new CommodityFlowReportExportPO();
		BeanUtils.copyProperties(cfreVo,commodityFlowReportExportPO);
		int type = reportExportPermissionService.getManagerType(commodityFlowReportExportPO.getUserid(), reporttype);
		//商品经理过滤 有数据就去掉之前的逻辑否则不用。
		List<String> names = commonMapper.getNoFilterGoodsManager(commodityFlowReportExportPO.getUsername());
		if(names!=null && names.size()>0){
			type = 1;
		}
		List<DwCommodityFlow> list = commodityFlowReportExportMapper.queryCommodityFlow(commodityFlowReportExportPO,1,10,type);
		JSONArray objectArr = JSON.parseArray(JSONObject.toJSONString(list));
		for(int i = 0,len = objectArr.size() ; i<len ; i++){
			JSONObject object = objectArr.getJSONObject(i);
			List<String> itemList = Lists.newArrayList();
			for(String item : targetList){
				itemList.add(object.getString(item)==null?"":object.getString(item));
			}
			tableList.add(itemList);
		}
		System.out.println("3."+(System.currentTimeMillis()-start)/1000);
		resultMap.put("tableData",tableList );
		resultMap.put("rownum",list.size()>0?list.get(0).getCountNum():0);
		return ServerResponse.createBySuccess(resultMap);
	}
	
	
	public Object addDownloadTask(String reportid,HttpServletRequest request){
		DvReportExportStatus dvReportExportStatus = reportExportStatusMapper.queryReportStatusByReportId(reportid);
		CommodityFlowReportExportVO cfreVo = JSON.parseObject(dvReportExportStatus.getReportparam(), CommodityFlowReportExportVO.class);
		return download(cfreVo,request);
	}
	
	public Object addDownloadTask(CommodityFlowReportExportVO cfreVo,HttpServletRequest request){

		if(reportDownloadStatusMapper.queryReportByName(cfreVo.getUserid(), cfreVo.getReportName())>0){
			return ServerResponse.createByErrorMessage("已存在相同名称的报表，请重新命名!");
		}else{
			return download(cfreVo,request);
		}
	}
	
	public Object download(CommodityFlowReportExportVO cfreVo,HttpServletRequest request){
		//商品流向默认按商品编号搜索
		cfreVo.setSearchName(searchName);
		context.initDBExcelDefinitionReader("export");
		CommodityFlowReportExportPO cfrePo= new CommodityFlowReportExportPO();
		String[] targetArr = cfreVo.getTarget();
		List<String> targetList = getDefaultTargetList(targetArr);
		//mybatis无法判断数组长度
		List<String> searchItemList = Lists.newArrayList();
			if(StringUtils.isNotBlank(cfreVo.getUploadFileId())){
				int partSize=1000;
				String uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(cfreVo.getUploadFileId()).getFileName();
				File uploadFile = new File(uploadPath+uploadFileName);
				FileInputStream readStream = null;
				try {
					readStream = new FileInputStream(uploadFile);
					List<List<Object>> datas =POIExcelUtils.readExcel(readStream, 0);
					for(int i=0,len=datas.size();i<len;i++){
						if(i>0&&datas.get(i).get(0)!=null){
							searchItemList.add(datas.get(i).get(0).toString());
						}
					}
					dvReportSearchTmpMapper.deleteDvReportSearchTmp(cfreVo.getUserid());
					if(searchItemList.size()>partSize){
						int part = searchItemList.size()/partSize;
						for(int i=0;i<part;i++){
							List<String> tmpList  = searchItemList.subList(0, partSize);
							dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(cfreVo.getUserid(), tmpList);
							searchItemList.subList(0, partSize).clear();
						}
					}
					if(searchItemList.size()>0){
						dvReportSearchTmpMapper.insertDvReportSearchTmpByBatch(cfreVo.getUserid(), searchItemList);
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
			cfreVo.setSearchValue(searchItemList.size()>0?searchItemList.toArray(new String[searchItemList.size()]):null);
			BeanUtils.copyProperties(cfreVo, cfrePo);
			//用户ID，UUID组成下载ID
			String downloadId = cfreVo.getUserid()+UUID.randomUUID().toString().replaceAll("-", "");
			String downloadReportName = cfreVo.getReportName();
			String fileName =downloadId+downloadReportName+".xlsx";
			int type = reportExportPermissionService.getManagerType(cfrePo.getUserid(), reporttype);
			//商品经理过滤 有数据就去掉之前的逻辑否则不用。
			List<String> names = commonMapper.getNoFilterGoodsManager(cfrePo.getUsername());
			if(names!=null && names.size()>0){
				type = 1;
			}
			
			DvReportDownloadStatusEntity dvReportDownloadStatusEntity = new DvReportDownloadStatusEntity();
			dvReportDownloadStatusEntity.setDownloadId(downloadId);
			dvReportDownloadStatusEntity.setFileName(fileName);
			dvReportDownloadStatusEntity.setReportName(downloadReportName);
			dvReportDownloadStatusEntity.setReportType(reporttype);
			dvReportDownloadStatusEntity.setUserid(cfreVo.getUserid());
			dvReportDownloadStatusEntity.setUsername(cfreVo.getUsername());
			dvReportDownloadStatusEntity.setUserrealname(cfreVo.getUserrealname());
			dvReportDownloadStatusEntity.setTargetName(JSON.toJSONString(reportCommonService.getTargetNameList(targetArr, reporttype)));
			dvReportDownloadStatusEntity.setIp(DvUtils.getIp(request));
			
			BeanUtils.copyProperties(cfreVo, cfrePo);
			int rownum = commodityFlowReportExportMapper.queryCommodityFlowCount(cfrePo,type);
			if(reportDownloadStatusMapper.insertReport(dvReportDownloadStatusEntity)>0){
				DownloadTask downloadTask = new DownloadTask(fileName,targetList,rownum, cfrePo, dvReportDownloadStatusEntity,type);
				new Thread(downloadTask).start();  
				return ServerResponse.createBySuccessMessage("加入下载列表成功！");
			}else{
				return ServerResponse.createByErrorMessage("加入下载列表失败！");
			}
	}
		
		class DownloadTask implements Runnable{
			public  boolean flag = true; 
			private String fileName;
			private Integer rownum;
			private List<String> targetList;
			private CommodityFlowReportExportPO cfrePo;
			private DvReportDownloadStatusEntity dvReportDownloadStatusEntity;
			private int type;
			public DownloadTask(String fileName,List<String> targetList,int rownum,CommodityFlowReportExportPO cfrePo,DvReportDownloadStatusEntity dvReportDownloadStatusEntity,int type){
				this.fileName = fileName;
				this.rownum = rownum;
				this.cfrePo = cfrePo;
				this.type = type;
				this.targetList = targetList;
				this.dvReportDownloadStatusEntity = dvReportDownloadStatusEntity;
			}
			public void stopThread(){
				flag = false;
			}
			@Override
			public void run() {
				while (flag){
					OutputStream ops = null;
					ExcelExportResult exportResult = null;
					try {
						ops = new FileOutputStream(downloadPath+fileName);
						exportResult = context.createEmptyExcelForPart(reporttype,DwCommodityFlow.class,targetList);
						int pageSize = 40000;
						int totalPage = (int)Math.ceil(rownum/pageSize);
						totalPage += rownum%pageSize>0?1:0; 
						for(int i=1;i<=totalPage;i++){
							exportResult.append(commodityFlowReportExportMapper.queryCommodityFlow(cfrePo, i, pageSize,type));
						}	
					Workbook workbook = exportResult.build();
					workbook.write(ops);
					ops.close();
					workbook.close();
					dvReportDownloadStatusEntity.setEndDate(new Date());
					dvReportDownloadStatusEntity.setTimeConsume(Math.round(dvReportDownloadStatusEntity.getEndDate().getTime()-dvReportDownloadStatusEntity.getBeginDate().getTime())/1000);
					dvReportDownloadStatusEntity.setDownloadFlag(1);
					reportDownloadStatusMapper.updateReportById(dvReportDownloadStatusEntity);
					
					this.stopThread();
					} catch (Exception e) {
						System.out.println("CommodityFlowReprot awaitTermination interrupted: " + e);  
						this.stopThread();
					}
				}
			}
		}
	public Object addOrUpdateReport(CommodityFlowReportExportVO cfreVo){
		String reportid = cfreVo.getReportid();
		List<String> targetNameList = Lists.newArrayList();
		String[] targetArr = cfreVo.getTarget();
		List<DvExcelFieldDefinitionEntity> excelFieldDefinitionList = reportCommonService.getExcelFieldDefinitionList(reporttype);
		for(String target : targetArr){
			for(DvExcelFieldDefinitionEntity dvExcelFieldDefinitionItem : excelFieldDefinitionList){
				String entityName = dvExcelFieldDefinitionItem.getEntityName();
				String targetName = dvExcelFieldDefinitionItem.getTitle();
					if(target.equals(entityName)){
						targetNameList.add(targetName);
					}
			}
		}
		int type = reportExportPermissionService.getManagerType(cfreVo.getUserid(), reporttype);
		CommodityFlowReportManageVO commodityFlowReportManageVO = new CommodityFlowReportManageVO();
		//权限修改了平台ID,对应的平台名称也要变
		commodityFlowReportManageVO.setSourcename(reportCommonService.getOrderSourceName(cfreVo.getOrdersource(), commonService.getOrderSource( type, cfreVo.getUserid(),reporttype)));
		commodityFlowReportManageVO.setReportid(reportid);
		commodityFlowReportManageVO.setReportName(cfreVo.getReportName());
		commodityFlowReportManageVO.setTargetName(targetNameList);
		commodityFlowReportManageVO.setUpdate(cfreVo.getUpdate());
		commodityFlowReportManageVO.setBeginDate(cfreVo.getBeginDate());
		commodityFlowReportManageVO.setEndDate(cfreVo.getEndDate());
		commodityFlowReportManageVO.setSaveTime(DvDateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		if(StringUtils.isNotBlank(cfreVo.getUploadFileId())){
			String uploadFileName = reportImportStatusMapper.queryReportStatusByUUID(cfreVo.getUploadFileId()).getReportName();
			cfreVo.setUploadFileName(uploadFileName);
			commodityFlowReportManageVO.setUploadFileName(cfreVo.getUploadFileName());
		}
		if(StringUtils.isNotBlank(reportid)){
			DvReportExportStatus dvReportExportStatus = new DvReportExportStatus();
			dvReportExportStatus.setReportid(reportid);
			dvReportExportStatus.setReportName(cfreVo.getReportName());
			dvReportExportStatus.setReportparam(JSON.toJSONString(cfreVo));
			dvReportExportStatus.setReportManageParam(JSON.toJSONString(commodityFlowReportManageVO));
			if(reportExportStatusMapper.updateReportStatus(dvReportExportStatus)>0){
				return ServerResponse.createBySuccessMessage("修改报表成功!");
			}else{
				return ServerResponse.createBySuccessMessage("修改报表失败!");
			}
		}else{
			//加入报表前需要根据用户ID，报表名去判断是否有重名的报表
			if(reportExportStatusMapper.queryReport(cfreVo.getUserid(), cfreVo.getReportName())>0){
				return ServerResponse.createByErrorMessage("已存在相同名称的报表，请重新命名!");
			}else{
				reportid = reporttype+String.valueOf(System.currentTimeMillis())+String.valueOf((int)((Math.random()*9+1)*100000));
				cfreVo.setReportid(reportid);
				commodityFlowReportManageVO.setReportid(reportid);
				DvReportExportStatus dvReportExportStatus = new DvReportExportStatus();
				BeanUtils.copyProperties(cfreVo, dvReportExportStatus);
				dvReportExportStatus.setCreatetime(new Date());
				dvReportExportStatus.setReportparam(JSON.toJSONString(cfreVo));
				dvReportExportStatus.setReportManageParam(JSON.toJSONString(commodityFlowReportManageVO));
				dvReportExportStatus.setReporttype(reporttype);
				dvReportExportStatus.setFileName(cfreVo.getUploadFileId()+cfreVo.getUploadFileName());
				if(reportExportStatusMapper.insertReportStatus(dvReportExportStatus)>0){
					return ServerResponse.createBySuccessMessage("添加报表成功!");
				}else{
					return ServerResponse.createByErrorMessage("添加报表失败!");
				}
			}
		}
	}
	private List<String> getDefaultTargetList(String[] targetArr){
		List<String> targetList = Lists.newArrayList();
		String[] defaultArr = new String[]{"warecode"};
		for(String defaultItem : defaultArr){
			targetList.add(defaultItem);
		}
		for(String targetItem : targetArr){
			targetList.add(targetItem);
		}
		return targetList;
	}
}
